<?php

class InventoryClientsManager implements InventoryManager
{
    private $selectCount;

    public function SelectAll()
    {
        $query = "SELECT * FROM client";
        $result = Core::DBConn()->Query($query);
        $this->selectCount = $result->Count();
        return array_map("self::CreateInventoryClient", $result->GetAllAssoc());
    }

    public static function SelectById($id)
    {
        $query = "SELECT * FROM client WHERE client_id = %s";
        $db = Core::DBConn();
        $queryResult = $db->Query($query, $db->Quote($id));
        return self::CreateInventoryClient($queryResult->GetAssoc());
    }

    public static function SelectByInventoryId($inventoryId)
    {
        $query = "SELECT client.* FROM client, inventory_rent
                  WHERE
                    client.client_id = inventory_rent.client_id
                      AND
                    inventory_rent.inventory_id = %s;";
        $db = Core::DBConn();
        $queryResult = $db->Query($query, $db->Quote($inventoryId));
        return array_map("self::CreateInventoryClient", $queryResult->GetAllAssoc());

    }

    public function GetSelectCount()
    {
        return $this->selectCount;
    }

    public function Insert(InventoryClient $inventoryClient)
    {
        $query = "INSERT INTO client
                  SET
                    name = @name,
                    birthday = @birthday,
                    phone_number = @phone_number,
                    photo_link = @photo_link;";
        $db = Core::DBConn();
        $substitutions = array(
            '@name' => $db->Quote($inventoryClient->GetName()),
            '@birthday' => $db->Quote($inventoryClient->GetBirthday()),
            '@phone_number' => $db->Quote($inventoryClient->GetPhoneNumber()),
            '@photo_link' => $db->Quote($inventoryClient->GetPhotoLink())
        );
        $db->Query($query, $substitutions);
    }

    public function Update(InventoryClient $inventoryClient)
    {
        $query = "UPDATE client
                  SET
                    name = @name,
                    birthday = @birthday,
                    phone_number = @phone_number,
                    photo_link = @photo_link
                  WHERE
                    client_id = @id;";
        $db = Core::DBConn();
        $substitutions = array(
            '@id' => $db->Quote($inventoryClient->GetId()),
            '@name' => $db->Quote($inventoryClient->GetName()),
            '@birthday' => $db->Quote($inventoryClient->GetBirthday()),
            '@phone_number' => $db->Quote($inventoryClient->GetPhoneNumber()),
            '@photo_link' => $db->Quote($inventoryClient->GetPhotoLink())
        );
        $db->Query($query, $substitutions);
    }

    public function Delete($itemId)
    {
        $query = "DELETE FROM client WHERE client_id = %s";
        $db = Core::DBConn();
        $db->Query($query, $db->Quote($itemId));
    }

    private static function CreateInventoryClient($dbRecord)
    {
        $inventoryClient = new InventoryClient();
        $inventoryClient->SetId($dbRecord['client_id']);
        $inventoryClient->SetName($dbRecord['name']);
        $inventoryClient->SetBirthday($dbRecord['birthday']);
        $inventoryClient->SetPhoneNumber($dbRecord['phone_number']);
        $photoLink = ($dbRecord['photo_link']) ? $dbRecord['photo_link'] : "";
        $inventoryClient->SetPhotoLink($photoLink);
        return $inventoryClient;
    }


}